Java3D & gl4java

Inhaltsverzeichnisseite

 

Cg

 

Cg ist erst Ende

letzten Jahres

erschienen und

eine der sogenannten

Shadersprachen.

Shadersprachen gibt

es aber nicht erst seit

es Pixel- und Vertex-

shader auf den

Grafikkarten gibt,

diese haben nur alles

etwas beschleunigt.

Beispiele fuer

fruehere Sprachen

sind z. B. die Stanford Shading language oder RenderMan von Pixar, mit

Hilfe welcher z.B. Toy Story entstanden ist.

Cg ist sehr stark an C angelehnt, hat sich aber einige Ideen aus diesen

Sprachen, C++ und Java geholt. Diese Sprache vereinfacht die Programmierung

der Shader erheblich, da eine C-aehnliche Sprache wesentlich mehr Komfort

beim Programmieren bietet als Assembler, in welcher die Shader zuvor

programmiert  werden mussten.

Anbei ist Beispielcode in Cg, welcher einfachen Nebel erzeugt. Zum Vergleich hier

das Stueck Code in Assembler aus dem Beispiel mit dem Triceratops.

 

/*********************************************************************NVMH4****

Path:  SDK\MEDIA\programs\cg_SimpleFog

File:  SimpleFog_dx8ps.cg

 

Copyright NVIDIA Corporation 2002

TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THIS SOFTWARE IS PROVIDED

*AS IS* AND NVIDIA AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES, EITHER EXPRESS

OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY

AND FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL NVIDIA OR ITS SUPPLIERS

BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT, OR CONSEQUENTIAL DAMAGES

WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS,

BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR ANY OTHER PECUNIARY LOSS)

ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF NVIDIA HAS

BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

 

 

 

Comments:

 

 

 

******************************************************************************/

 

 

#include "Constants.h"

 

 

struct VToF

{

    float4 COL0 : COLOR0;       

    float4 COL1 : COLOR1;       

};

 

 

fragout main(    VToF IN,

                uniform sampler2D    baseTexture            : texunit0,

                uniform float4        C_HEIGHT_FOG_COLOR    : CG_HEIGHT_FOG_COLOR

                )

{

    fragout OUT;

 

    //fetch base color

    float4 tex_color = tex2D( baseTexture );

 

    // Add height-based fog color to ( base texture * lighting )

    OUT.col =    tex_color * IN.COL0 +  IN.COL1 * C_HEIGHT_FOG_COLOR;

   

    return( OUT );

}

/*********************************************************************NVMH4****

Path:  SDK\MEDIA\programs\cg_SimpleFog

File:  SimpleFog_dx8vs.cg

 

Copyright NVIDIA Corporation 2002

TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THIS SOFTWARE IS PROVIDED

*AS IS* AND NVIDIA AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES, EITHER EXPRESS

OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY

AND FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL NVIDIA OR ITS SUPPLIERS

BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT, OR CONSEQUENTIAL DAMAGES

WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS,

BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR ANY OTHER PECUNIARY LOSS)

ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF NVIDIA HAS

BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

 

 

 

Comments:

 

 

This vertex shader performs the calculations for two fog effects.  The first

outputs a fog factor which varies with the vertices' screen depth.  This is

output to FOG for use in the standard D3D fog processing.

 

The second fog term varies with the terrain height (modelspace x coordinate)

and is written to the specular color for later addition in a pixel shader.

 

Enjoy!

NVIDIA Technical Developer Relations

 

******************************************************************************/

 

 

#include "Constants.h"

 

 

struct appdata

{

    float4 Position        : POSITION;

    float3 Normal        : NORMAL;

    float4 Diffuse        : DIFFUSE;

    float2 TexCoord0    : TEXCOORD0;

};

 

 

// This demo does not use the Cg D3D runtime to associate

//  variables with hardware registers or automaticaly bind

//  values.

// You must be very carefull when specifying : semantic names

//  as typos will not create warnings or errors.

// Shader Constant register semantics must use a capital 'C'!

 

 

struct vpconn

{

    float4 HPosition    : POSITION;

    float4 Color0        : COLOR0;

    float4 Color1        : COLOR1;

    float  Fog            : FOG;

    float2 TexCoord0    : TEXCOORD0;

    float2 TexCoord1    : TEXCOORD1;

};

 

 

vpconn main( appdata IN,

 

                uniform float4 C_ZEROONE            : CG_ZEROONE,

                uniform float4 C_LIGHT_POS_OSPACE    : CG_LIGHT_POS_OSPACE,

                uniform float4 C_LIGHT_CONST        : CG_LIGHT_CONST,

                uniform float4 C_FOGPARAMS            : CG_FOGPARAMS,

                uniform float4 C_HEIGHT_FOG_PARAMS    : CG_HEIGHT_FOG_PARAMS,

 

                uniform float4x4 WorldViewProj        : CG_WORLDVIEWPROJ_0

            )

{

 

    vpconn OUT;

 

    // Transform position to hclip space;

    float4 hcpos;

    hcpos = mul( WorldViewProj, IN.Position );

 

    OUT.HPosition = hcpos;

 

    // Generate a fog value

    // scale by fog parameters:

    //   c[C_FOGPARAMS].x = fog start

    //   c[C_FOGPARAMS].y = fog end

    //   c[C_FOGPARAMS].z = range

    //

    // based on:  cameraspace depth (z) - fog start

    // 1.0 - (z - fog start) * 1/range

    //   Because Fog = 1.0 means no fog, and fog = 0.0 means full fog

 

    OUT.Fog = C_ZEROONE.y - ( hcpos.z - C_FOGPARAMS.x ) / C_FOGPARAMS.z;

 

    // Height-based fog

    // Compute fog factor from world space x coord == height

    // Multiply the factor by itself a few times to get a sharp   

    //   falloff.  Could also use the expp instruction here.

    // Output this to specular color oD1 for use in the pixel

    //   program

 

    float htfog = IN.Position.x * C_HEIGHT_FOG_PARAMS.x;

    OUT.Color1 = htfog * htfog.xxxx;

 

    // Point lighting

    // based on normalized vector from vertex to light

 

    float3 vec_vert_to_light;

    vec_vert_to_light = normalize( C_LIGHT_POS_OSPACE.xyz - IN.Position.xyz );

 

    // Dot the vectors for the diffuse component from the

    //  point light

    // Point light is not attenuated

 

    float light;

    light = dot( vec_vert_to_light, IN.Normal );

 

    // clamp() negative values of the dotproduct to zero

    light = clamp( light, C_ZEROONE.x, C_ZEROONE.y );

 

    // output light + ambient

    OUT.Color0 = ( light + C_LIGHT_CONST.x ).xxxx;        // x holds ambient

 

    // Output texture coordinates

    OUT.TexCoord0.xy = IN.TexCoord0;

    OUT.TexCoord1.xy = IN.TexCoord0;

 

    return( OUT );

}